#ifndef motors_h
#define motors_h

//TODO: Utilizar la estructura motors para pasar informacion
/**
 * @defgroup nxbot_motors MOTORS Library
 * @ingroup nxbot_high_level
 * @code #include <motors.h> @endcode
 * @brief Motor library for controlling the velocities and for reading the odometers of the two motors of NXBot.
 *
 * This module uses timer0 of the ATmega1281 microcontroller for generating the desired PWM signals. Timer0 is configured in <i>Free Running mode</i>,
 * and it is constantly compared with two registers (OC0 and OC1), in order to toggle the logical state of two pins, where the PWM signals to the
 * motors are connected. Another two signals are used in order to set the desired direction of the motor. For FORWARD direction use <b>positive</b>
 * values, for BACKWARD direction use <b>negative</b> values.
 * This library does not need to have the interrupts in order to function correctly, it is not an interrupt-driven module.
 * 
 */
 
/**@{*/
#include "structures.h"
#include "pwm.h"
#include "encoders.h"

#define TICS_PER_ROT 362 
#define WHEEL_RADIUS 33 // radius wheel in mm
#define PI 3.14159
#define DIR_RIGHT 4
#define DIR_LEFT 3
#define MOTOR_RIGHT 1
#define MOTOR_LEFT 0
#define MAX_VEL 100
//relationship between sample frequency and velocity
/**
 * @brief Relationship between the sample frequency and the max velocity.
 * VEL_FACT =   
 */
#define VEL_FACT 320

/**
 * @brief Updates the <code>#velocities</code> of <code>#motors</code> structure with the actual %velocities.
 *
 * The #velocities are measured in % units in respect to the max velocity. The max velocity is dependant on the actual voltage of the %battery.
 * The measuring follows the function:
 * @f[
 * vel[%] = sqrt{1}
 * \f]
 * @param vels The pointer to the <code>#velocities</code> structure.
 * @param sampleFreq The frequency of the call to this function, in Hz.
 */
void motors_getActualVels(motors_t *motors, unsigned int sampleFreq);

/**
 * \brief Initializes all the hardware used for the motors (%encoders and pwm).
 *
 * Configures the pwm frequency for the motors and starts the odometers.
 * \param vels The frequency for pwm.
 */
void motors_init(unsigned char freq);

/**
 * \brief Sets the desired velocity for each motor.
 *
 * The velocity is defined as a percentage(%) of the max. velocity, which is dependant on the voltage of the %battery.
 * \param vel The desired velocity. It is an int betwen 0 and 100. Larger values are truncated to 100. 
 * If the parameter is negative, the motor will run backwards.
 * \param motor The selected motor. It has to be one of the following constants:
 * <ul>
 * <li>MOTOR_LEFT
 * <li>MOTOR_RIGHT
 * </ul>
 */
void motors_setVel(int vel,unsigned char motor);


/**
 * @brief Updates the <code>#odometers</code> of <code>#motors</code> structure with the actual %encoders reading.
 *
 * The #position is measured in pulses.
 *
 * @param motors The pointer to the <code>#motors</code> structure.
 */
void motors_getActualPos(motors_t *motors);
#define motors_getActualLPos() encoders_readLeft();
#define motors_getActualRPos() encoders_readRight();
#define motors_setDirection(l,r) encoders_setDirection(l,r);

 
/**@}*/
#endif
